package com.dpmc.service;
 
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import com.common.util.ObjectUtils;
import com.core.service.AppBaseService; 
import com.dpmc.entity.*; 

import javax.ejb.Stateless;

/**
 *  规则管理
 */
@Stateless
public class SVC_GZGL extends AppBaseService implements SVC_GZGLRemote, SVC_GZGLLocal {

	@Override
	public Map ADD_YJFPGZ(Map param) { 
		String opid = getOpid(param);
		Date optime = getOpTime(param); 
		
		TB_GZGL_YJFPGZ yjfpgz =  ObjectUtils.mapToJavaBean(param, TB_GZGL_YJFPGZ.class);
		yjfpgz.setID(yjfpgz.uuid());
		yjfpgz.setCREATE_TIME(optime);
		yjfpgz.setOP_ITEM(optime);
		yjfpgz.setOPID(opid);
		getJpaDao().save(yjfpgz); 
		
		Collection<Map> list_fz=ObjectUtils.getMapValue(param, "LIST_TB_GZGL_YJFPGZ_FZ", Collection.class);
		if(list_fz!=null){
			for(Map item :list_fz){
				TB_GZGL_YJFPGZ_FZ fz =  ObjectUtils.mapToJavaBean(item, TB_GZGL_YJFPGZ_FZ.class);
				fz.setID(fz.uuid());
				fz.setYJFPGZ_ID(yjfpgz.getID());
				fz.setCREATE_TIME(optime);
				fz.setOP_ITEM(optime);
				fz.setOPID(opid);
				getJpaDao().save(fz);
			}
		} 
		
		Collection<Map> list_yjfpgzx=ObjectUtils.getMapValue(param, "LIST_TB_GZGL_YJFPGZX", Collection.class);
		if(list_yjfpgzx!=null){
			for(Map item :list_yjfpgzx){
				TB_GZGL_YJFPGZX yjfpgzx =  ObjectUtils.mapToJavaBean(item, TB_GZGL_YJFPGZX.class);
				yjfpgzx.setID(yjfpgzx.uuid());
				yjfpgzx.setYJFPGZ_ID(yjfpgz.getID());
				yjfpgzx.setCREATE_TIME(optime);
				yjfpgzx.setOP_ITEM(optime);
				yjfpgzx.setOPID(opid);
				getJpaDao().save(yjfpgzx);
			}
		}
		
		Map retMap =ObjectUtils.javaBeanToMap(yjfpgz); 
		return retMap;
	}

	@Override
	public Map UPD_YJFPGZ(Map param) {
		System.out.println("UPD_YJFPGZFZDY="+param);
		String opid = getOpid(param);
		Date optime = getOpTime(param);
		TB_GZGL_YJFPGZ yjfpgz =  ObjectUtils.mapToJavaBean(param, TB_GZGL_YJFPGZ.class); 
		TB_GZGL_YJFPGZ yjfpgz_po = getJpaDao().find(TB_GZGL_YJFPGZ.class, yjfpgz.getID());
		ObjectUtils.copyJavaBeanToBean(yjfpgz, yjfpgz_po, "CREATE_TIME");
		getJpaDao().update(yjfpgz_po);
		
		StringBuffer hql = new StringBuffer("select o from TB_GZGL_YJFPGZX o")
                                   .append(" where YJFPGZ_ID = '").append(yjfpgz.getID()).append("'");
		Collection<TB_GZGL_YJFPGZX> po_list = (Collection<TB_GZGL_YJFPGZX>) getResultList(hql.toString());
		for(TB_GZGL_YJFPGZX item:po_list){
			item.setSTATUS(TB_GZGL_YJFPGZX.STATUS_DEAD);
			getJpaDao().update(item);
		}
		
		
		Collection<Map> list_yjfpgzx=ObjectUtils.getMapValue(param, "LIST_TB_GZGL_YJFPGZX", Collection.class); 
		if(list_yjfpgzx!=null){
			for(Map item :list_yjfpgzx){
				TB_GZGL_YJFPGZX yjfpgzx =  ObjectUtils.mapToJavaBean(item, TB_GZGL_YJFPGZX.class);
				yjfpgzx.setOP_ITEM(optime);
				yjfpgzx.setOPID(opid); 
				yjfpgzx.setYJFPGZ_ID(yjfpgz.getID());
				if(yjfpgzx.getID()==null){
					yjfpgzx.setID(yjfpgzx.uuid()); 
					yjfpgzx.setCREATE_TIME(optime); 
					getJpaDao().save(yjfpgzx);
				}else{
					TB_GZGL_YJFPGZX yjfpgzx_po = getJpaDao().find(TB_GZGL_YJFPGZX.class, yjfpgzx.getID());
					ObjectUtils.copyJavaBeanToBean(yjfpgzx, yjfpgzx_po, "CREATE_TIME");
					yjfpgzx_po.setSTATUS(TB_GZGL_YJFPGZX.STATUS_ACTIVITY);
				} 
			} 
		} 
		
		hql = new StringBuffer("select o from TB_GZGL_YJFPGZ_FZ o")
		        .append(" where YJFPGZ_ID = '").append(yjfpgz.getID()).append("'");
		Collection<TB_GZGL_YJFPGZ_FZ> po_fz_list = (Collection<TB_GZGL_YJFPGZ_FZ>) getResultList(hql.toString());
		for(TB_GZGL_YJFPGZ_FZ item:po_fz_list){
			item.setSTATUS(TB_GZGL_YJFPGZX.STATUS_DEAD);
			getJpaDao().update(item);
		}
		
		Collection<Map> list_fz=ObjectUtils.getMapValue(param, "LIST_TB_GZGL_YJFPGZ_FZ", Collection.class); 
		if(list_fz!=null){
			for(Map item :list_fz){
				TB_GZGL_YJFPGZ_FZ fz =  ObjectUtils.mapToJavaBean(item, TB_GZGL_YJFPGZ_FZ.class);  
				fz.setOP_ITEM(optime);
				fz.setOPID(opid);
				fz.setYJFPGZ_ID(yjfpgz.getID());
				if(fz.getID()==null){
					fz.setID(fz.uuid()); 
					fz.setCREATE_TIME(optime); 
					getJpaDao().save(fz);
				} else{
					TB_GZGL_YJFPGZ_FZ fz_po = getJpaDao().find(TB_GZGL_YJFPGZ_FZ.class, fz.getID());
					ObjectUtils.copyJavaBeanToBean(fz, fz_po, "CREATE_TIME");
					fz_po.setSTATUS(TB_GZGL_YJFPGZX.STATUS_ACTIVITY);
				}
			}
		}
		
		for(TB_GZGL_YJFPGZX item:po_list){ 
			getJpaDao().update(item);
		}
		
		Map retMap =ObjectUtils.javaBeanToMap(yjfpgz_po); 
		return retMap;
	}

	@Override
	public void DEL_YJFPGZ(Map param) {
		TB_GZGL_YJFPGZ tdo =  ObjectUtils.mapToJavaBean(param, TB_GZGL_YJFPGZ.class);
		
		TB_GZGL_YJFPGZ po =getJpaDao().find(TB_GZGL_YJFPGZ.class, tdo.getID());
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_GZGL_YJFPGZ.STATUS_DEAD);
		getJpaDao().update(po);
	}

	@Override
	public Map ADD_YJFPGZFZDY(Map param) { 
		Map STATEMENT_OBJ_TJ = ObjectUtils.getMapValue(param, "STATEMENT_OBJ_TJ", Map.class);  
		if(STATEMENT_OBJ_TJ!=null){
			copySysParam(param,STATEMENT_OBJ_TJ);
		    STATEMENT_OBJ_TJ = (Map) getServiceInterface().doService("SVC_BASE", "ADD_STATEMENT", STATEMENT_OBJ_TJ); 
	    	param.put("STATEMENT_ID_TJ", STATEMENT_OBJ_TJ.get("ID"));
		}
		
		Map STATEMENT_OBJ_QZ = ObjectUtils.getMapValue(param, "STATEMENT_OBJ_QZ", Map.class); 
		if(STATEMENT_OBJ_QZ!=null){
			copySysParam(param,STATEMENT_OBJ_QZ);
			STATEMENT_OBJ_QZ = (Map) getServiceInterface().doService("SVC_BASE", "ADD_STATEMENT", STATEMENT_OBJ_QZ);
			param.put("STATEMENT_ID_QZ", STATEMENT_OBJ_QZ.get("ID"));
		} 
		
		TB_GZGL_YJFPGZ_FZDY dto =  ObjectUtils.mapToJavaBean(param, TB_GZGL_YJFPGZ_FZDY.class);
		dto.setID(dto.uuid());
		dto.setOP_ITEM(getOpTime(param));
		dto.setOPID(getOpid(param));
		getJpaDao().save(dto);
		return ObjectUtils.javaBeanToMap(dto);
	}

	@Override
	public Map UPD_YJFPGZFZDY(Map param) { 
		Map STATEMENT_OBJ_TJ = ObjectUtils.getMapValue(param, "STATEMENT_OBJ_TJ", Map.class); 
		if(STATEMENT_OBJ_TJ!=null){
		   copySysParam(param,STATEMENT_OBJ_TJ);
		   STATEMENT_OBJ_TJ = (Map) getServiceInterface().doService("SVC_BASE", "UPD_STATEMENT", STATEMENT_OBJ_TJ); 
		}
		
		Map STATEMENT_OBJ_QZ = ObjectUtils.getMapValue(param, "STATEMENT_OBJ_QZ", Map.class); 
		if(STATEMENT_OBJ_QZ!=null){
			copySysParam(param,STATEMENT_OBJ_QZ);
		   STATEMENT_OBJ_QZ = (Map) getServiceInterface().doService("SVC_BASE", "UPD_STATEMENT", STATEMENT_OBJ_QZ); 
		}
		
		TB_GZGL_YJFPGZ_FZDY dto =  ObjectUtils.mapToJavaBean(param, TB_GZGL_YJFPGZ_FZDY.class);
		dto.setOP_ITEM(getOpTime(param));
		dto.setOPID(getOpid(param));
		TB_GZGL_YJFPGZ_FZDY po = getJpaDao().find(TB_GZGL_YJFPGZ_FZDY.class, dto.getID());
		ObjectUtils.copyJavaBeanToBean(dto, po, "ID");
		getJpaDao().update(po);
		return  ObjectUtils.javaBeanToMap(po);
	}

	@Override
	public void DEL_YJFPGZFZDY(Map param) {
		String id =ObjectUtils.getMapValue(param, "ID", String.class); 
		TB_GZGL_YJFPGZ_FZDY po = getJpaDao().find(TB_GZGL_YJFPGZ_FZDY.class, id);
		po.setOP_ITEM(getOpTime(param));
		po.setOPID(getOpid(param));
		po.setSTATUS(TB_GZGL_YJFPGZ_FZDY.STATUS_DEAD);
		getJpaDao().update(po);
		
		Map tmp = new HashMap();
		copySysParam(param,tmp);
		if(po.getSTATEMENT_ID_QZ()!=null){
		   tmp.put("ID", po.getSTATEMENT_ID_QZ());
		   getServiceInterface().doService("SVC_BASE", "DEL_STATEMENT", tmp); 
		}
		if(po.getSTATEMENT_ID_TJ()!=null){
			tmp.put("ID", po.getSTATEMENT_ID_TJ());
			getServiceInterface().doService("SVC_BASE", "DEL_STATEMENT", tmp); 
	    }
	}

 

}
